/**
 * @param {number[]} nums
 * @return {number}
 */
var wiggleMaxLength = function(nums) {
  if(nums.length < 2) return nums.length;
  let upStart = 1, downStart = 1, needUp = true, needDown = true;
  for(let i = 1; i < nums.length; i++) {
    if(needUp && nums[i] > nums[i - 1]) {
      needUp = !needUp;
      upStart++;
    } else if(!needUp && nums[i] < nums[i - 1]) {
      needUp = !needUp;
      upStart++;
    }

    if(needDown && nums[i] < nums[i - 1]) {
      needDown = !needDown;
      downStart++;
    } else if(!needDown && nums[i] > nums[i - 1]) {
      needDown = !needDown;
      downStart++;
    }
  }
  return Math.max(upStart, downStart);
};
let nums = [1,7,4,9,2,5];
console.log(wiggleMaxLength(nums));
nums = [1,17,5,10,13,15,10,5,16,8];
console.log(wiggleMaxLength(nums));
nums = [1,2,3,4,5,6,7,8,9];
console.log(wiggleMaxLength(nums));
